<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        height: 2000px;
      }
    </style>
  </head>
  <body>
    <div></div>
    <img data-src="1.jpg" alt="" />

    <img src="1.jpg" alt="" />

    <script>
      const img = document.querySelector('img')
      // 如何知道图片是否该加载了，，，如何知道图片是否在可视区
      const observer = new IntersectionObserver(function ([
        { isIntersecting },
      ]) {
        console.log('监听')
        if (isIntersecting) {
          img.src = img.dataset.src
          observer.unobserve(img)
        }
      })
      observer.observe(img)
    </script>
  </body>
</html>
